My Matlab projects

This is a collection of some of the most interesting Matlab projects

Economics - Finance - Mathematics

Oligopoli model using game theory

Defining inputs
%Defining parameters cost functions
c_11=2;c_12=1;c_13=0;
c_21=40;c_22=1;c_23=1;
c_31=.25;c_32=0;c_33=.5;
%Defining cost functions
syms q Q positive
C_1(q)=c_11+c_12*q^2+c_13*q^3
C_1(q) = 
C_2(q)=c_21+c_22*q^2+c_23*q^3
C_2(q) = 
C_3(q)=c_31+c_32*q^2+c_33*q^3
C_3(q) = 
%Defining market demand function
a=60;
b=5;
D(Q)=a-b*Q
D(Q) = 
Computing Marginal Profit
%Defining firm profit function
syms q1 q2 q3 positive
Q_tot=[q1,q2,q3];
% computing marginal profit
MP_1=marginal_profit(q1,C_1,Q_tot,D)
MP_1(q1) = 
MP_2=marginal_profit(q2,C_2,Q_tot,D)
MP_2(q2) = 
MP_3=marginal_profit(q3,C_3,Q_tot,D)
MP_3(q3) = 
% reaction functions
eq1=MP_1(q1)==0;
eq2=MP_2(q2)==0;
eq3=MP_3(q3)==0;
eqs=[eq1,eq2,eq3];
eqs'
ans = 
S=solve(eqs,[q1 q2 q3]);
% Extracting the solutions
qu_1=(S.q1);
qu_2=(S.q2);
qu_3=(S.q3);
Equilibrium quantities and market quantity
% Equilibrium quantities and market distribution
q_1=double(qu_1)
q_1 = 3.1921
q_2=double(qu_2)
q_2 = 1.8014
q_3=double(qu_3)
q_3 = 2.5374
pie([q_1,q_2,q_3])
Q_mkt=sum([q_1 q_2 q_3])
Q_mkt = 7.5310
Reaction functions
quantity_1(q2,q3)=solve(MP_1==0,q1)
Warning: Solutions are valid under the following conditions: q2 + q3 < 12. To include parameters and conditions in the solution, specify the 'ReturnConditions' value as 'true'.
quantity_1(q2, q3) = 
quantity_2(q1,q3)=solve(MP_2==0,q2)
Warning: Solutions are valid under the following conditions: q1 + q3 < 12. To include parameters and conditions in the solution, specify the 'ReturnConditions' value as 'true'.
quantity_2(q1, q3) = 
quantity_3(q1,q2)=solve(MP_3==0,q3)
Warning: Solutions are valid under the following conditions: q1 + q2 < 12. To include parameters and conditions in the solution, specify the 'ReturnConditions' value as 'true'.
quantity_3(q1, q2) = 
fplot(quantity_1(q2,q_3),[0,20])
hold on
fplot(quantity_1(q_2,q3),[0,20])
plot(q_2,q_1,'o')
plot(q_3,q_1,'o')
hold off
xlabel("q")
ylabel("q1")
title("reaction functions firm 1")
legend("q2","q3",'Location',"best")
fplot(quantity_2(q1,q_3),[0,20])
hold on
fplot(quantity_2(q_1,q3),[0,20])
plot(q_1,q_2,'o')
plot(q_3,q_2,'o')
hold off
xlabel("q")
ylabel("q2")
title("reaction functions firm 2")
legend("q1","q3",'Location',"best")
fplot(quantity_3(q1,q_2),[0,20])
hold on
fplot(quantity_3(q_1,q2),[0,20])
plot(q_1,q_3,'o')
plot(q_2,q_3,'o')
hold off
xlabel("q")
ylabel("q3")
title("reaction functions firm 3")
legend("q1","q2",'Location',"best")
fplot((quantity_1(q2,q_3)),[0,20])
hold on
fplot(finverse(quantity_2(q1,q_3)),[0,20])
ylim([0 5])
xlabel("q2")
ylabel("q1")
plot(q_2,q_1,'o')
title("reaction functions firm 1-2")
hold off
fplot(quantity_1(q_2,q3),[0,20])
hold on
fplot(finverse(quantity_3(q1,q_2)),[0,20])
ylim([0 5])
xlabel("q3")
ylabel("q1")
plot(q_3,q_1,'o')
title("reaction functions firm 1-3")
hold off
fplot(quantity_2(q_1,q3),[0,20])
hold on
fplot(finverse(quantity_3(q_1,q2)),[0,20])
ylim([0 5])
xlabel("q3")
ylabel("q2")
plot(q_3,q_2,'o')
title("reaction functions firm 2-3")
hold off
clear variables

Application of Game theory

Computing Nash Equilibrium and minimax given game matrices using derivatives method and expectation method. The Jacobi derived from matching (s1,s2)-->(E(s1),E(s2)) has minimax on the opposite diagonal.
A = sym('a', [2 2])
A = 
B = sym('b', [2 2])
B = 
syms x y
s_1=[x;1-x]
s_1 = 
s_2=[y;1-y]
s_2 = 
E_1=s_1.'*A*s_2
E_1 = 
E_2=s_2.'*B*s_1
E_2 = 
%NE
c1=diff(E_1,x);
expand(c1)
ans = 
NE_pl2=solve(c1,y)
NE_pl2 = 
c2=diff(E_2,y);
expand(c2)
ans = 
NE_pl1=solve(c2,x)
NE_pl1 = 
%MinMax
c3=diff(E_1,y);
expand(c3)
ans = 
MinMaxx=solve(c3,x)
MinMaxx = 
c4=diff(E_2,x);
expand(c4)
ans = 
MinMaxy=solve(c4,y)
MinMaxy = 
eq_1=sum(A(:,1).*s_1- A(:,2).*s_1)==0
eq_1 = 
eq_2=sum(B(:,1).*s_2-B(:,2).*s_2)==0
eq_2 = 
MiMaX=solve(eq_1,x)
MiMaX = 
MiMaY=solve(eq_2,y)
MiMaY = 
t1=(MiMaX-MinMaxx)
t1 = 
0
t2=(MiMaY-MinMaxy)
t2 = 
0
3x3 case
A = sym('a', [3 3])
A = 
B = sym('b', [3 3])
B = 
syms x_1 x_2 y_1 y_2
s_1=[x_1;x_2;1-x_2-x_1]
s_1 = 
s_2=[y_1;y_2;1-y_1-y_2]
s_2 = 
E_1=s_1.'*A*s_2
E_1 = 
E_2=s_2.'*B*s_1
E_2 = 
dx1=diff(E_1,x_1)==0;
expand(dx1)
ans = 
dx2=diff(E_1,x_2)==0;
expand(dx2)
ans = 
NE_pl2=solve([dx1,dx2],[y_1,y_2])
NE_pl2 = struct with fields:
y_1: [1×1 sym] y_2: [1×1 sym]
NE_pl2.y_1
ans = 
NE_pl2.y_2
ans = 
dy1=diff(E_2,y_1)==0;
expand(dy1)
ans = 
dy2=diff(E_2,y_2)==0;
expand(dy2)
ans = 
NE_pl1=solve([dy1,dy2],[x_1,x_2])
NE_pl1 = struct with fields:
x_1: [1×1 sym] x_2: [1×1 sym]
NE_pl1.x_1
ans = 
NE_pl1.x_2
ans = 
%MinMax
dE1y1=diff(E_1,y_1)==0;
expand(dE1y1)
ans = 
dE1y2=diff(E_1,y_2)==0;
expand(dE1y2)
ans = 
MM_1=solve([dE1y1,dE1y2],[x_1,x_2])
MM_1 = struct with fields:
x_1: [1×1 sym] x_2: [1×1 sym]
w1=MM_1.x_1
w1 = 
w2=MM_1.x_2
w2 = 
dE2x1=diff(E_2,x_1)==0;
expand(dE2x1)
ans = 
dE2x2=diff(E_2,x_2)==0;
expand(dE2x2)
ans = 
MM_2=solve([dE2x1,dE2x2],[y_1,y_2])
MM_2 = struct with fields:
y_1: [1×1 sym] y_2: [1×1 sym]
w3=MM_2.y_1
w3 = 
w4=MM_2.y_2
w4 = 
%Standard method
eq3_1=sum(A(:,1).*s_1- A(:,2).*s_1)==0
eq3_1 = 
eq3_2=sum(A(:,1).*s_1- A(:,3).*s_1)==0
eq3_2 = 
eq3_3=sum(A(:,2).*s_1- A(:,3).*s_1)==0
eq3_3 = 
S1=solve([eq3_1,eq3_2],[x_1,x_2])
S1 = struct with fields:
x_1: [1×1 sym] x_2: [1×1 sym]
z1=S1.x_1
z1 = 
z2=S1.x_2
z2 = 
S2=solve([eq3_1,eq3_3],[x_1,x_2])
S2 = struct with fields:
x_1: [1×1 sym] x_2: [1×1 sym]
z3=S2.x_1
z3 = 
z4=S2.x_2
z4 = 
S3=solve([eq3_2,eq3_3],[x_1,x_2])
S3 = struct with fields:
x_1: [1×1 sym] x_2: [1×1 sym]
z5=S3.x_1
z5 = 
z6=S3.x_2
z6 = 
%Check for match
z1-w1
ans = 
0
z2-w2
ans = 
0
z3-w1
ans = 
0
z4-w2
ans = 
0
z5-w1
ans = 
0
z6-w2
ans = 
0
eq3b_1=sum(B(:,1).*s_2- B(:,2).*s_2)==0
eq3b_1 = 
eq3b_2=sum(B(:,1).*s_2- B(:,3).*s_2)==0
eq3b_2 = 
eq3b_3=sum(B(:,2).*s_2- B(:,3).*s_2)==0
eq3b_3 = 
S1b=solve([eq3b_1,eq3b_2],[y_1,y_2])
S1b = struct with fields:
y_1: [1×1 sym] y_2: [1×1 sym]
Z1=S1b.y_1
Z1 = 
Z2=S1b.y_2
Z2 = 
S2b=solve([eq3b_1,eq3b_3],[y_1,y_2])
S2b = struct with fields:
y_1: [1×1 sym] y_2: [1×1 sym]
Z3=S2b.y_1
Z3 = 
Z4=S2b.y_2
Z4 = 
S3b=solve([eq3b_2,eq3b_3],[y_1,y_2])
S3b = struct with fields:
y_1: [1×1 sym] y_2: [1×1 sym]
Z5=S3b.y_1
Z5 = 
Z6=S3b.y_2
Z6 = 
Z1-w3
ans = 
0
Z2-w4
ans = 
0
Z3-w3
ans = 
0
Z4-w4
ans = 
0
Z5-w3
ans = 
0
Z6-w4
ans = 
0
clear variables

Duration and analysis of change in price given change in yield

% Macaulay duration
format long
r=3.31/100 % interest rate
r =
0.033100000000000
t=11
t =
11
PV=zeros(t,1);
c=0/100 %coupon rate
c =
0
FV=1000 %face value
FV =
1000
C=c*100
C =
0
CF(1:t-1,1)=C;
CF(t)=C+FV;
PV=CF.*(1/(1+r))
PV = 11×1
102 ×
0 0 0 0 0 0 0 0 0 0
Time=(1:t)';
PVt=Time.*PV;
table(Time,CF,PV,PVt)
ans = 11×4 table
 TimeCFPVPVt
11000
22000
33000
44000
55000
66000
77000
88000
99000
1010000
111110009.679605072113059e+021.064756557932436e+04
sum_pvt=sum(PVt)
sum_pvt =
1.064756557932436e+04
durat=sum_pvt/sum(PV)
durat =
11
modified_duration=durat/(1+r)
modified_duration =
10.647565579324365
t=5
t =
5
CF=zeros(t,1)
CF = 5×1
0 0 0 0 0
FV=100
FV =
100
c=3.5/100
c =
0.035000000000000
C=c*FV
C =
3.500000000000000
CF(1:t-1,1)=C;
CF(t)=FV+C;
Rates=[2.34 2.65 2.92 3.14 3.31]'./100;
Time=(1:t)';
PV=CF.*(1+Rates).^-Time;
B=sum(PV) %bond price
B =
1.009934409558063e+02
Rate_p=Rates.*100;
table(Time,Rate_p,CF,PV)
ans = 5×4 table
 TimeRate_pCFPV
112.3400000000000003.5000000000000003.419972640218878
222.6500000000000003.5000000000000003.321621454258070
332.9200000000000003.5000000000000003.210470707425835
443.1400000000000013.5000000000000003.092854831143058
553.3100000000000001.035000000000000e+0287.948521322760485
%shift yield curve by rr
rr=1.05/100
rr =
0.010500000000000
Rates_shifted=Rates-rr;
PV_new=CF.*(1+Rates_shifted).^-Time;
B_new=sum(PV_new)
B_new =
1.059363552843600e+02
table(Time,Rates_shifted,CF,PV_new)
ans = 5×4 table
 TimeRates_shiftedCFPV_new
110.0129000000000003.5000000000000003.455425017277126
220.0160000000000003.5000000000000003.390631781263563
330.0187000000000003.5000000000000003.310770873777689
440.0209000000000003.5000000000000003.222071912521444
550.0226000000000001.035000000000000e+0292.557455699520162
%compute price elasticity
PE_factor=(CF.*Time.*(1+Rates).^-(Time+1))*(-1/B);
table(Time,Rates_shifted,CF,PV_new,PE_factor)
ans = 5×5 table
 TimeRates_shiftedCFPV_newPE_factor
110.0129000000000003.5000000000000003.455425017277126-0.033089031041881
220.0160000000000003.5000000000000003.390631781263563-0.064080812484821
330.0187000000000003.5000000000000003.310770873777689-0.092661007845546
440.0209000000000003.5000000000000003.222071912521444-0.118767941963760
550.0226000000000001.035000000000000e+0292.557455699520162-4.214664561499389
PE=sum(PE_factor) % price elasticity
PE =
-4.523263354835398
t=3
t =
3
r=5.2/100
r =
0.052000000000000
FV=100
FV =
100
c=4.88/100
c =
0.048800000000000
C=c*FV
C =
4.880000000000000
CF=zeros(t,1);
CF(1:t-1,1)=C;
CF(t)=FV+C;
Rates=zeros(t,1);
Rates(1:t,1)=r;
Time=(1:t)';
PV=CF.*(1+Rates).^-Time;
B=sum(PV) %bond price
B =
99.131816177129821
Rate_p=Rates.*100;
table(Time,Rate_p,CF,PV)
ans = 3×4 table
 TimeRate_pCFPV
11       26/5     122/25    1220/263   
22       26/5     122/25    2509/569   
33       26/5     2622/25    35583/395   
PVt=Time.*PV;
table(Time,CF,PV,PVt)
ans = 3×4 table
 TimeCFPVPVt
11       122/25    1220/263   1220/263   
22       122/25    2509/569   5018/569   
33       2622/25    35583/395   107830/399   
durat=sum(PVt)/B
durat =
2.861930741631810
modified_duration=durat/(1+r)
modified_duration =
2.720466484440884
% Convexity
PVtt1=PVt.*(Time+1);
CX=(1/(2*B))*(1/(1+r)^2)*sum(PVtt1)
CX =
5.089514735942870
delta_y=2.1/100
delta_y =
0.021000000000000
deltaB_1st=-B*modified_duration*delta_y
deltaB_1st =
-5.663380452484362
deltaB_2nd=deltaB_1st+B*CX*delta_y^2
deltaB_2nd =
-5.440881470382044
Rates_shift=Rates+delta_y;
PV_shift=CF.*(1+Rates_shift).^-Time;
B_shift=sum(PV_shift) %bond price
B_shift =
93.683804709963070
-B+B_shift
ans =
-5.448011467166751
clear variables

Derivation of the Green function, a very important tool in the theory of differential equations with applications to BLS theory

% Green Function
syms x t A B C D K p k
% initial conditions
% l<x<u a_11y(l)+a_12y'(l)=0 a_21y(u)+a_22y'(u)=0
M=[1 0;1 -2]
M = 2×2
1 0 1 -2
b_1=0
b_1 =
0
b_2=0
b_2 =
0
%p(x)=1/(1+x^2)
%p(x)=x/x
p(x)=1/x^3
p(x) = 
l=1
l =
1
u=2
u =
2
%FSS
y_1=x
y_1 = 
x
y_2=x^3
y_2 = 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Wronskian
W=[y_1 y_2;diff(y_1,x) diff(y_2,x)]
W = 
det(W)
ans = 
% Green function
G_1=A*y_1+B*y_2
G_1 = 
G_1s=subs(G_1,x,l)
G_1s = 
G_2=C*y_1+D*y_2
G_2 = 
G_2s=subs(G_2,x,u)
G_2s = 
eq_1=M(1,1)*G_1s+M(1,2)*subs(diff(G_1,x),x,l)
eq_1 = 
eq_2=M(2,1)*G_2s+M(2,2)*subs(diff(G_2,x),x,u)
eq_2 = 
sol_1A=solve(eq_1==b_1,A)
sol_1A = 
sol_1B=solve(eq_1==b_1,B)
sol_1B = 
sol_2C=solve(eq_2==b_2,C)
sol_2C = Empty sym: 0-by-1
sol_2D=solve(eq_2==b_2,D)
sol_2D = 
0
if isempty(sol_1A)==1
G_1n=subs(G_1,B,sol_1B)
end
if isempty(sol_1B)==1
G_1n=subs(G_1,A,sol_1A)
end
if isempty(sol_1B)==0 && isempty(sol_1A)==0
G_1n=subs(G_1,A,sol_1A) %sol in B
end
G_1n = 
if isempty(sol_2C)==1
G_2n=subs(G_2,D,sol_2D)
end
G_2n = 
if isempty(sol_2D)==1
G_2n=subs(G_2,C,sol_2C)
end
if isempty(sol_2D)==0 && isempty(sol_2C)==0
G_2n=subs(G_2,C,sol_2C) % sol in D
end
G_1n
G_1n = 
G_2n
G_2n = 
% Symmetrie assuming b_1=b_2=0
if isempty(sol_1A)==1
sy1=G_1n/A
end
if isempty(sol_1B)==1
sy1=G_1n/B
end
if isempty(sol_1B)==0 && isempty(sol_1A)==0
sy1=G_1n/B
end
sy1 = 
if isempty(sol_2C)==1
sy2=subs(G_2n,x,t)/C
end
sy2 = 
t
if isempty(sol_2D)==1
sy2=subs(G_2n,x,t)/D
end
if isempty(sol_2D)==0 && isempty(sol_2C)==0
sy2=simplify(subs(G_2n,x,t)/D)
end
sy1
sy1 = 
sy2
sy2 = 
t
U_x=simplify(K*sy1)
U_x = 
L_t=simplify(K*sy2)
L_t = 
U_t=subs(U_x,x,t)
U_t = 
L_t
L_t = 
if isempty(sol_1A)==1
G_1nn=subs(G_1n,A,L_t)
end
if isempty(sol_1B)==1
G_1nn=subs(G_1n,B,L_t)
end
if isempty(sol_1B)==0 && isempty(sol_1A)==0
G_1nn=subs(G_1n,B,L_t)
end
G_1nn = 
if isempty(sol_2C)==1
G_2nn=subs(G_2n,C,U_t)
end
G_2nn = 
if isempty(sol_2D)==1
G_2nn=subs(G_2n,D,U_t)
end
if isempty(sol_2D)==0 && isempty(sol_2C)==0
G_2nn=subs(G_2n,D,U_t)
end
G_1nn
G_1nn = 
G_2nn
G_2nn = 
eqc=diff(G_2nn,x)-diff(G_1nn,x)
eqc = 
eqcc=subs(eqc,x,t)==1/p(t)
eqcc = 
solk=simplify(solve(eqcc,K)) % k should be a number
solk = 
G_1final=simplify(subs(G_1nn,K,solk))
G_1final = 
G_2final=simplify(subs(G_2nn,K,solk))
G_2final = 
if simplify(subs(eqc,x,t))==0
disp('Error: 4th property')
simplify(subs(eqc,x,t))
end
clear variables

Identification of parameters of the BLS equation

syms A B S a b c t sigma d alpha beta
format rational
V=A*S-B*S^2*exp(-a*t-b*t^2-c*t^3-d*t^4)
V = 
r=alpha*t+beta*t^3
r = 
V_t=diff(V,t)
V_t = 
V_S=diff(V,S)
V_S = 
V_SS=diff(V_S,S)
V_SS = 
eq=V_t+sigma^2/2*S^2*V_SS+r*S*V_S-r*V
eq = 
simp=simplify(eq)
simp = 
collect(simp,t)
ans = 
v=solve(V_S==0,S)
v = 
subs(V,S,v(1))
ans = 
simplify(subs(V,S,v(1)))
ans = 
clear variables

Bi-dimensional Newton method to solve system of equations

Solve: f=0, g=0
f=@(x,y)x-7*x-sin(y);
g=@(x,y)cos(x)-3*x.^2*y+2*y;
tol=10e-10;
MaxI=100;
Newton_multi(f,g,[0;0],MaxI,tol)
ans =
270/3359 -694/1379
clear variables

Isoquants - Isocosts and Economy of scale

plot isoquants/isocosts and cost function/marginal costs/ average costs given a production function f and W wage R rental rate
clear
clc
syms f(x,y) C W R Q
a=0.25;%parameters of production function
b=0.5;
A=1;
disp('Wage W; Rental rate R');
Wage W; Rental rate R
w0=[1 1]%choose values for parameters wage(W) and rental rate(R)
w0 =
1 1
disp('Production function');
Production function
f(x,y)=A.*(x.^a).*(y.^b)%production function (Cobb-Douglas type)
f(x, y) = 
disp('Cost function expressed through labour(x) and capital(y)');
Cost function expressed through labour(x) and capital(y)
cost(x,y)=W.*x+R.*y %cost function
cost(x, y) = 
MPL=diff(f,x);
MPK=diff(f,y);
[solx,soly] = solve(MPL.*R == W.*MPK, x.*W+y.*R == C);
Warning: Possibly spurious solutions.
q=(f(solx,soly));%it expresses labour(x) and capital trough Cost variable C
disp('Quantity respect to costs, wage and rental rate');
Quantity respect to costs, wage and rental rate
q=q(1)%%
q = 
F=subs(f,[W R],w0);
COST=subs(cost,[W R],w0);
q=subs(q,[W R],w0)
q = 
F=matlabFunction(F);
%COST=matlabFunction(COST);
f2=figure;
hold on
grid on
for i=1:10
Cost=10*i; %choose Cost
Quantity=subs(q,C,Cost); %correspondent output q(Cost)
fimplicit(F-Quantity,[0 Cost]);%isoquant at quantity q(Cost)
fimplicit(COST-Cost,[0 Cost]);%isocost
[rootx,rooty]=(solve(F-Quantity,COST-Cost));%point of production is the
%intersection between isoquant and isocost
plot(real(rootx),real(rooty),'or')
xlim([0 Cost]);
ylim([0 Cost]);
end
ylabel('capital');
xlabel('labour');
title('isoquants and isocost lines');
hold off
ExplicitCost=finverse(q);
Warning: Possibly spurious solutions.
disp('explicit costs:')
explicit costs:
subs(ExplicitCost,C,Q)
ans = 
MarginalCost=diff(ExplicitCost);
disp('marginal costs:')
marginal costs:
subs(MarginalCost,C,Q)
ans = 
AverageCost=ExplicitCost./C;
disp('average costs:')
average costs:
subs(AverageCost,C,Q)
ans = 
f1=figure;
fplot(ExplicitCost,[0 10]);
grid on
xlabel('quantity');
ylabel('Costs');
hold on
fplot(MarginalCost,[0 10]);
hold on
fplot(AverageCost,[0 10]);
legend('Cost function','Marginal Cost','Average Costs','Location','northwest');
hold off
clear variables

Econometrics analysis between access to banking services and gpd growth for emerging economies

load('CW2_MAndB.mat')
load('CW_MAndB1.mat')
data=DataExtractFromWorldDevelopmentIndicators5;
%data=DataExtractFromWorldDevelopmentIndicators6;
head(data)
ans = 8×29 table
 CountryNameCountryCodeSeriesNameSeriesCodeYR1996YR1997YR1998YR1999YR2000YR2001YR2002YR2003YR2004YR2005YR2006YR2007YR2008YR2009YR2010YR2011YR2012YR2013YR2014YR2015YR2016YR2017YR2018YR2019YR2020
1ArgentinaARGInflation, GDP deflator (annual %)NY.GDP.DEFL.KD.ZG-183/3494  -1067/2300  -1163/682   -854/465   306/295   -492/449   9686/317   3663/349   5913/322   3477/337   6527/475   4228/283   2572/111   2891/188   5668/271   4077/172   6449/289   7951/332   16798/417   1329/50    2755/67    3485/134   13147/323   6181/120   ..
2ArgentinaARGDomestic credit to private sector by banks (% of GDP)FD.AST.PRVT.GD.ZS2405/121   10412/483   13093/555   4079/169   5120/221   2842/141   4051/268   5215/491   3696/389   3323/319   5697/494   7643/606   10073/852   16703/1392  529/43    3859/285   4774/325   4161/274   3865/287   2253/160   3465/262   5745/373   0/0     0/0     ..
3ArgentinaARGGDP per capita growth (annual %)NY.GDP.PCAP.KD.ZG1868/437   1675/244   563/210   -1987/446   -2683/1431  -2059/378   -2857/241   3109/405   3769/478   1600/207   18933/2722  523/66    1336/441   -2351/343   7282/783   1269/265   -1137/530   1977/1562  -2573/719   1747/1072  -989/318   1075/668   -2604/751   -2479/792   ..
4ArgentinaARGGDP per capita, PPP (constant 2017 international $)NY.GDP.PCAP.PP.KD126734/7     135434/7     99332/5     56944/3     204880/11    193720/11    93139/6     83574/5     234426/13    97133/5     124667/6     67273/3     69311/3     258241/12    493951/21    147887/6     313548/13    73273/3     306154/13    239341/10    92759/4     282755/12    227459/10    396611/18    ..
5ArgentinaARGGeneral government final consumption expenditure (% of GDP)NE.CON.GOVT.ZS3738/299   1556/129   3586/287   1537/112   4535/329   18389/1299  15343/1254  2585/226   4557/410   5221/430   2113/170   3650/281   2604/191   6123/385   3427/226   5397/344   9671/581   2689/160   6763/399   15637/864   10222/579   10264/581   5515/343   2398/157   ..
6ArgentinaARGSchool enrollment, secondary (% net)SE.SEC.NENR0/0     0/0     1637/22    4881/64    3218/41    9395/119   7487/95    14368/183   3220/41    13171/169   10193/131   5671/73    3854/49    1853/23    9934/121   12612/151   4037/47    3571/41    10253/117   7429/84    2337/26    13801/152   0/0     0/0     ..
7ArgentinaARGTrade (% of GDP)NE.TRD.GNFS.ZS11635/541   8191/351   41960/1797  9665/452   2217/98    1923/88    3883/93    3089/76    9400/231   3163/78    9421/233   2989/73    6020/149   1192/35    2413/69    3415/97    6899/226   16427/560   1676/59    2451/109   5558/213   7567/300   6969/227   7159/221   ..
8BrazilBRAInflation, GDP deflator (annual %)NY.GDP.DEFL.KD.ZG4153/225   5619/727   5013/1018  3052/381   185/33    2229/271   3106/317   11456/813   938/121   1999/269   2791/412   11513/1788  3766/429   2333/319   3681/437   3943/474   699/88    1651/220   1075/137   1029/136   1799/222   2277/626   1715/522   2235/538   ..
size(data)
ans =
140 29
categories(data.SeriesName)
ans = 7×1 cell array
'Domestic credit to private …
'GDP per capita growth (annu…
'GDP per capita, PPP (consta…
'General government final co…
'Inflation, GDP deflator (an…
'School enrollment, secondar…
'Trade (% of GDP)'
country=categories(data.CountryName)
country = 20×1 cell array
'Argentina'
'Brazil'
'Chile'
'Colombia'
'Dominican Republic'
'Egypt, Arab Rep.'
'Indonesia'
'Israel'
'Korea, Rep.'
'Malaysia'
country_abb=categories(data.CountryCode)
country_abb = 20×1 cell array
'ARE'
'ARG'
'BRA'
'CHL'
'COL'
'DOM'
'EGY'
'IDN'
'ISR'
'KOR'
%country(5)=[];
%country(10)=[];
for i=1:20
table{i}=data(1+(i-1)*7:7*i,:);
table{i}(:,4:8)=[];
table{i}(:,end)=[];
table{i}(:,1:2)=[];
table{i}.Properties.VariableNames{1}=country{i};
variables=table{1}.Argentina;
end
variables
variables = 7×1 categorical array
Inflation, GDP deflator (ann…
Domestic credit to private s…
GDP per capita growth (annua…
GDP per capita, PPP (constan…
General government final con…
School enrollment, secondary…
Trade (% of GDP)
table{20}
ans = 7×21 table
 UruguayYR2000YR2001YR2002YR2003YR2004YR2005YR2006YR2007YR2008YR2009YR2010YR2011YR2012YR2013YR2014YR2015YR2016YR2017YR2018YR2019
1Inflation, GDP deflator (annual %)2210/627   412/905   295/2706  1259/698   4703/749   1412/403   245/32    1279/858   1549/1404  3337/1617  2981/522   4803/712   4001/3327  665/629   7633/3138  1391/517   419/136   1016/281   621/308   6919/4448  
2Domestic credit to private sector by banks (% of GDP)3161/120   8535/364   8681/391   12611/631   11373/635   5167/271   7909/380   12525/509   6935/233   12396/413   3238/107   4911/152   5447/160   21010/557   4703/115   7501/171   2911/68    3393/80    4929/112   4187/93    
3GDP per capita growth (annual %)979/807   -708/1079  1913/447   3941/1258  5137/1284  4381/817   1365/206   5626/737   3292/399   424/1477  835/112   8633/1579  2855/542   3623/739   1213/929   1231/627   1587/655   951/1147  2199/997   1005/1961  
4GDP per capita, PPP (constant 2017 international $)160562/25    19141/3     33267/5     41171/6     99909/14    270685/36    32069/4     163956/19    121436/13    196729/21    171130/17    63701/6     122937/11    11724       142525/12    181654/15    198459/16    212611/17    102259/8     115631/9     
5General government final consumption expenditure (% of GDP)1855/162   1160/99    4533/406   2154/187   2240/199   18769/1640  31975/2929  1976/189   1566/151   2385/208   3463/329   4404/425   1435/132   6176/537   4750/383   7909/610   4423/336   2801/212   3815/291   1091/82    
6School enrollment, secondary (% net)12277/198   20216/311   10739/163   0/0     7990/121   4369/65    13389/193   6778/93    6721/90    9404/121   3186/41    7656/97    9189/122   11599/145   1511/19    18766/237   12421/150   15063/176   10092/113   0/0     
7Trade (% of GDP)6539/184   10344/295   10751/305   11927/317   9184/219   19085/403   5541/107   6070/109   4850/83    6447/134   11213/217   4759/85    8998/171   12397/249   8293/177   1942/43    52878/1165  1758/37    9636/197   18943/402   
table{5}
ans = 7×21 table
 Dominican RepublicYR2000YR2001YR2002YR2003YR2004YR2005YR2006YR2007YR2008YR2009YR2010YR2011YR2012YR2013YR2014YR2015YR2016YR2017YR2018YR2019
1Inflation, GDP deflator (annual %)7327/657   1456/241   3269/594   2969/735   3535/444   2050/349   1067/167   741/128   2111/342   2112/535   1359/299   5206/891   6229/1532  283/185   1933/437   2609/936   242/45    2350/353   5399/1087  3223/967   
2Domestic credit to private sector by banks (% of GDP)6074/431   1765/152   5974/453   2871/214   14419/1127  4161/299   2111/129   1873/105   7086/419   14219/788   9368/511   1184/61    6233/313   3951/178   31609/1441  5471/229   4692/181   7991/297   2791/104   8087/281   
3GDP per capita growth (annual %)2653/771   -2476/1381  -2775/1978  130/1799  2173/871   773/889   767/257   835/1067  -417/1166  -7415/1111  463/128   902/405   2755/1226  23/781   925/617   723/359   1977/1177  662/707   2705/2727  -2438/1983  
4GDP per capita, PPP (constant 2017 international $)142265/8     122250/7     120535/7     120622/7     141293/8     89076/5     146775/8     203395/11    55273/3     51584/3     142533/8     127494/7     130359/7     93141/5     245797/13    77153/4     254959/13    158367/8     79969/4     138225/7     
5General government final consumption expenditure (% of GDP)4377/460   1413/143   3819/368   1633/148   2475/236   14057/1336  5286/511   11602/1115  1309/122   3301/277   5097/433   5653/480   2019/169   2548/209   7541/618   3391/275   709/59    2128/183   3733/320   2356/203   
6School enrollment, secondary (% net)13406/239   0/0     8579/142   9173/145   31795/488   8068/121   10922/161   7376/107   7030/101   7456/107   4135/59    2049/29    5387/75    5970/79    6213/80    18646/233   16407/202   8846/109   0/0     0/0     
7Trade (% of GDP)5453/104   13348/283   4483/96    7079/141   21617/370   11287/181   8470/151   7213/127   8551/148   1735/31    7352/121   4189/66    7629/116   22509/353   3573/55    33804/475   27320/359   7951/103   2333/29    9772/125   
for i=1:20
T=table{i};
table{i}.Average=mean(T{:,2:end},2,'omitnan');
table{i}.Average(4)=table{i}.YR2000(4);
end
table{1}
ans = 7×22 table
 ArgentinaYR2000YR2001YR2002YR2003YR2004YR2005YR2006YR2007YR2008YR2009YR2010YR2011YR2012YR2013YR2014YR2015YR2016YR2017YR2018YR2019Average
1Inflation, GDP deflator (annual %)306/295   -492/449   9686/317   3663/349   5913/322   3477/337   6527/475   4228/283   2572/111   2891/188   5668/271   4077/172   6449/289   7951/332   16798/417   1329/50    2755/67    3485/134   13147/323   6181/120   3246/143   
2Domestic credit to private sector by banks (% of GDP)5120/221   2842/141   4051/268   5215/491   3696/389   3323/319   5697/494   7643/606   10073/852   16703/1392  529/43    3859/285   4774/325   4161/274   3865/287   2253/160   3465/262   5745/373   0/0     0/0     3857/279   
3GDP per capita growth (annual %)-2683/1431  -2059/378   -2857/241   3109/405   3769/478   1600/207   18933/2722  523/66    1336/441   -2351/343   7282/783   1269/265   -1137/530   1977/1562  -2573/719   1747/1072  -989/318   1075/668   -2604/751   -2479/792   747/815   
4GDP per capita, PPP (constant 2017 international $)204880/11    193720/11    93139/6     83574/5     234426/13    97133/5     124667/6     67273/3     69311/3     258241/12    493951/21    147887/6     313548/13    73273/3     306154/13    239341/10    92759/4     282755/12    227459/10    396611/18    204880/11    
5General government final consumption expenditure (% of GDP)4535/329   18389/1299  15343/1254  2585/226   4557/410   5221/430   2113/170   3650/281   2604/191   6123/385   3427/226   5397/344   9671/581   2689/160   6763/399   15637/864   10222/579   10264/581   5515/343   2398/157   3210/217   
6School enrollment, secondary (% net)3218/41    9395/119   7487/95    14368/183   3220/41    13171/169   10193/131   5671/73    3854/49    1853/23    9934/121   12612/151   4037/47    3571/41    10253/117   7429/84    2337/26    13801/152   0/0     0/0     5020/61    
7Trade (% of GDP)2217/98    1923/88    3883/93    3089/76    9400/231   3163/78    9421/233   2989/73    6020/149   1192/35    2413/69    3415/97    6899/226   16427/560   1676/59    2451/109   5558/213   7567/300   6969/227   7159/221   6560/199   
table{5}
ans = 7×22 table
 Dominican RepublicYR2000YR2001YR2002YR2003YR2004YR2005YR2006YR2007YR2008YR2009YR2010YR2011YR2012YR2013YR2014YR2015YR2016YR2017YR2018YR2019Average
1Inflation, GDP deflator (annual %)7327/657   1456/241   3269/594   2969/735   3535/444   2050/349   1067/167   741/128   2111/342   2112/535   1359/299   5206/891   6229/1532  283/185   1933/437   2609/936   242/45    2350/353   5399/1087  3223/967   133/25    
2Domestic credit to private sector by banks (% of GDP)6074/431   1765/152   5974/453   2871/214   14419/1127  4161/299   2111/129   1873/105   7086/419   14219/788   9368/511   1184/61    6233/313   3951/178   31609/1441  5471/229   4692/181   7991/297   2791/104   8087/281   2160/113   
3GDP per capita growth (annual %)2653/771   -2476/1381  -2775/1978  130/1799  2173/871   773/889   767/257   835/1067  -417/1166  -7415/1111  463/128   902/405   2755/1226  23/781   925/617   723/359   1977/1177  662/707   2705/2727  -2438/1983  1777/2463  
4GDP per capita, PPP (constant 2017 international $)142265/8     122250/7     120535/7     120622/7     141293/8     89076/5     146775/8     203395/11    55273/3     51584/3     142533/8     127494/7     130359/7     93141/5     245797/13    77153/4     254959/13    158367/8     79969/4     138225/7     142265/8     
5General government final consumption expenditure (% of GDP)4377/460   1413/143   3819/368   1633/148   2475/236   14057/1336  5286/511   11602/1115  1309/122   3301/277   5097/433   5653/480   2019/169   2548/209   7541/618   3391/275   709/59    2128/183   3733/320   2356/203   5003/446   
6School enrollment, secondary (% net)13406/239   0/0     8579/142   9173/145   31795/488   8068/121   10922/161   7376/107   7030/101   7456/107   4135/59    2049/29    5387/75    5970/79    6213/80    18646/233   16407/202   8846/109   0/0     0/0     28349/403   
7Trade (% of GDP)5453/104   13348/283   4483/96    7079/141   21617/370   11287/181   8470/151   7213/127   8551/148   1735/31    7352/121   4189/66    7629/116   22509/353   3573/55    33804/475   27320/359   7951/103   2333/29    9772/125   11959/192   
table{20}
ans = 7×22 table
 UruguayYR2000YR2001YR2002YR2003YR2004YR2005YR2006YR2007YR2008YR2009YR2010YR2011YR2012YR2013YR2014YR2015YR2016YR2017YR2018YR2019Average
1Inflation, GDP deflator (annual %)2210/627   412/905   295/2706  1259/698   4703/749   1412/403   245/32    1279/858   1549/1404  3337/1617  2981/522   4803/712   4001/3327  665/629   7633/3138  1391/517   419/136   1016/281   621/308   6919/4448  1618/557   
2Domestic credit to private sector by banks (% of GDP)3161/120   8535/364   8681/391   12611/631   11373/635   5167/271   7909/380   12525/509   6935/233   12396/413   3238/107   4911/152   5447/160   21010/557   4703/115   7501/171   2911/68    3393/80    4929/112   4187/93    251/8     
3GDP per capita growth (annual %)979/807   -708/1079  1913/447   3941/1258  5137/1284  4381/817   1365/206   5626/737   3292/399   424/1477  835/112   8633/1579  2855/542   3623/739   1213/929   1231/627   1587/655   951/1147  2199/997   1005/1961  1192/329   
4GDP per capita, PPP (constant 2017 international $)160562/25    19141/3     33267/5     41171/6     99909/14    270685/36    32069/4     163956/19    121436/13    196729/21    171130/17    63701/6     122937/11    11724       142525/12    181654/15    198459/16    212611/17    102259/8     115631/9     160562/25    
5General government final consumption expenditure (% of GDP)1855/162   1160/99    4533/406   2154/187   2240/199   18769/1640  31975/2929  1976/189   1566/151   2385/208   3463/329   4404/425   1435/132   6176/537   4750/383   7909/610   4423/336   2801/212   3815/291   1091/82    3043/261   
6School enrollment, secondary (% net)12277/198   20216/311   10739/163   0/0     7990/121   4369/65    13389/193   6778/93    6721/90    9404/121   3186/41    7656/97    9189/122   11599/145   1511/19    18766/237   12421/150   15063/176   10092/113   0/0     11093/148   
7Trade (% of GDP)6539/184   10344/295   10751/305   11927/317   9184/219   19085/403   5541/107   6070/109   4850/83    6447/134   11213/217   4759/85    8998/171   12397/249   8293/177   1942/43    52878/1165  1758/37    9636/197   18943/402   6424/137   
Mtable=[];
for i=1:20
t=table{i}.Average';
ve=t(4);
t(4)=[];
M(i,:)=[t 0];
M(i,7)=ve;
end
Mtable=array2table(M);
vars=cellstr(variables);
nam=vars(4);
vars(4)=[];
vars(7)=nam;
Mtable.Properties.VariableNames=vars;
Mtable.Properties.RowNames=cellstr(country);
Mtable
Mtable = 20×7 table
 Inflation, GDP deflator (annual %)Domestic credit to private sector by banks (% of GDP)GDP per capita growth (annual %)General government final consumption expenditure (% of GDP)School enrollment, secondary (% net)Trade (% of GDP)GDP per capita, PPP (constant 2017 international $)
1 Argentina3246/143   3857/279   747/815   3210/217   5020/61    6560/199   204880/11    
2 Brazil961/129   1496/31    2956/2179  4070/211   5808/73    2882/111   81180/7     
3 Chile9377/1079  2937/109   26875/6891  4152/481   8451/131   10665/206   130853/23    
4 Colombia563/297   24203/194   9608/2697  3471/248   28111/295   23511/296   206893/9     
5 Dominican Republic133/25    2160/113   1777/2463  5003/446   28349/403   11959/192   142265/8     
6 Egypt, Arab Rep.6081/463   11042/277   854/229   2847/160   23780/259   22305/424   160761/11    
7 Indonesia601/116   2116/55    693/884   841/36    12957/148   26712/349   209776/5     
8 Israel3758/243   1389/35    1577/466   1559/113   94605/1193  8843/178   92819/6     
9 Korea, Rep.1415/717   983/14    1477/1012  5264/223   16342/165   15100/219   726065/23    
10 Malaysia689/210   3332/29    6551/2005  15555/1256  5967/79    18272/109   318401/20    
11 Mexico9167/1950  12021/169   899/344   5362/439   15775/182   6769/103   75609/5     
12 Panama827/130   8201/241   13638/5455  2153/147   9521/129   9272/253   210074/23    
13 Peru3557/335   4231/109   2125/907   4699/410   21815/271   9017/188   54213/7     
14 Russian Federation14996/11397 14441/135   1360/421   578/57    7873/79    17931/49    392074/7     
15 Saudi Arabia1533/230   4501/66    1027/839   1853/94    50669/754   31347/532   70622/7     
16 Singapore1929/250   6368/207   257/107   1635/127   6788/89    12935/261   132961/10    
17 South Africa2955/664   8617/145   -2017/1192  1996/197   12231/136   9881/69    819959/8     
18 Turkey5135/1766  8392/109   419/104   2177/174   6107/95    16330/127   159616/11    
19 United Arab Emirates2046/233   6616/275   2476/677   21827/2240  21137/344   2825/46    65255/7     
20 Uruguay1618/557   251/8     1192/329   3043/261   11093/148   6424/137   160562/25    
new_name=["INF","BNK","GRO","GOV","SSE","TRD","INIT"];
Warning: Table variable names were truncated to the length namelengthmax.
MMT=Mtable;
MMT.Properties.VariableNames=new_name;
MMT
MMT = 20×7 table
 INFBNKGROGOVSSETRDINIT
1 Argentina3246/143   3857/279   747/815   3210/217   5020/61    6560/199   204880/11    
2 Brazil961/129   1496/31    2956/2179  4070/211   5808/73    2882/111   81180/7     
3 Chile9377/1079  2937/109   26875/6891  4152/481   8451/131   10665/206   130853/23    
4 Colombia563/297   24203/194   9608/2697  3471/248   28111/295   23511/296   206893/9     
5 Dominican Republic133/25    2160/113   1777/2463  5003/446   28349/403   11959/192   142265/8     
6 Egypt, Arab Rep.6081/463   11042/277   854/229   2847/160   23780/259   22305/424   160761/11    
7 Indonesia601/116   2116/55    693/884   841/36    12957/148   26712/349   209776/5     
8 Israel3758/243   1389/35    1577/466   1559/113   94605/1193  8843/178   92819/6     
9 Korea, Rep.1415/717   983/14    1477/1012  5264/223   16342/165   15100/219   726065/23    
10 Malaysia689/210   3332/29    6551/2005  15555/1256  5967/79    18272/109   318401/20    
11 Mexico9167/1950  12021/169   899/344   5362/439   15775/182   6769/103   75609/5     
12 Panama827/130   8201/241   13638/5455  2153/147   9521/129   9272/253   210074/23    
13 Peru3557/335   4231/109   2125/907   4699/410   21815/271   9017/188   54213/7     
14 Russian Federation14996/11397 14441/135   1360/421   578/57    7873/79    17931/49    392074/7     
15 Saudi Arabia1533/230   4501/66    1027/839   1853/94    50669/754   31347/532   70622/7     
16 Singapore1929/250   6368/207   257/107   1635/127   6788/89    12935/261   132961/10    
17 South Africa2955/664   8617/145   -2017/1192  1996/197   12231/136   9881/69    819959/8     
18 Turkey5135/1766  8392/109   419/104   2177/174   6107/95    16330/127   159616/11    
19 United Arab Emirates2046/233   6616/275   2476/677   21827/2240  21137/344   2825/46    65255/7     
20 Uruguay1618/557   251/8     1192/329   3043/261   11093/148   6424/137   160562/25    
corr(M)
ans =
1 -402/665 -179/3548 77/8220 -527/3529 -281/622 -365/1372 -402/665 1 264/1357 191/7877 1359/3133 821/1313 495/1846 -179/3548 264/1357 1 -367/1261 -309/1331 707/9665 -877/1393 77/8220 191/7877 -367/1261 1 260/753 -603/2123 -45/1514 -527/3529 1359/3133 -309/1331 260/753 1 466/1263 577/1061 -281/622 821/1313 707/9665 -603/2123 466/1263 1 509/936 -365/1372 495/1846 -877/1393 -45/1514 577/1061 509/936 1
y=MMT.GRO;
XX=M;
XX(:,3)=[];
X=[ones(size(y)) XX];
[b,bint,r,rint,stats] = regress(y,X)
b =
4550/2113 -141/11156 278/46981 -1307/12782 242/8479 43/6437 -33/510839
bint =
-5545/3392 1521/256 -470/3577 654/6163 -127/7094 121/4069 -455/1929 92/2933 -353/10844 243/2711 -69/19679 218/12925 -31/329808 -18/511289
r =
-3421/3866 -617/868 2035/2677 470/1303 -1739/1083 959/739 451/809 2073/2060 562/6917 -681/1348 -705/1198 -338/8925 -6359/7467 -814/4705 -441/494 -256/1519 -68/9547 338/345 577/636 1605/3376
rint =
-772/351 974/2267 -4545/1706 866/697 -793/713 566/215 -467/475 1529/897 -1999/619 41/2284 -595/1494 1416/473 -1149/1097 3263/1509 -478/593 1508/535 -1166/741 1493/860 -2921/1333 829/702 -2075/839 1269/979 -4886/2351 1588/793 -1096/397 2815/2662 -646/805 755/1654 -1807/739 989/1499 -2976/1345 407/217 -965/1801 133/255 -1094/1501 871/324 -764/829 736/269 -193/146 10066/4429
stats =
948/1327 2997/553 45/8579 4775/5269
mdl=fitlm(XX,y)
mdl =
Linear regression model: y ~ 1 + x1 + x2 + x3 + x4 + x5 + x6 Estimated Coefficients: Estimate SE tStat pValue _________ __________ ________ __________ (Intercept) 2.1533 1.7534 1.2281 0.24119 x1 -0.012639 0.05497 -0.22992 0.82173 x2 0.0059173 0.011026 0.53668 0.60055 x3 -0.10225 0.061851 -1.6532 0.12222 x4 0.028541 0.028279 1.0093 0.33127 x5 0.0066801 0.0047151 1.4167 0.18007 x6 -6.46e-05 1.3606e-05 -4.7478 0.00038094 Number of observations: 20, Error degrees of freedom: 13 Root Mean Squared Error: 0.952 R-squared: 0.714, Adjusted R-Squared: 0.583 F-statistic vs. constant model: 5.42, p-value = 0.00525
mdl.Rsquared.Adjusted
ans =
448/769
mdl.Rsquared.Ordinary
ans =
948/1327
XXX=XX(:,2:end);
mdl1=fitlm(XXX,y)
mdl1 =
Linear regression model: y ~ 1 + x1 + x2 + x3 + x4 + x5 Estimated Coefficients: Estimate SE tStat pValue ___________ __________ _______ __________ (Intercept) 2.0947 1.6751 1.2505 0.23161 x1 0.0072028 0.009176 0.78496 0.44556 x2 -0.10046 0.059247 -1.6957 0.11206 x3 0.026708 0.026198 1.0195 0.32528 x4 0.0067589 0.0045408 1.4885 0.1588 x5 -6.3936e-05 1.2839e-05 -4.9798 0.00020193 Number of observations: 20, Error degrees of freedom: 14 Root Mean Squared Error: 0.919 R-squared: 0.713, Adjusted R-Squared: 0.611 F-statistic vs. constant model: 6.96, p-value = 0.00184
mdl1.Rsquared.Adjusted
ans =
802/1313
mdl1.Rsquared.Ordinary
ans =
1644/2305
XXXX=XXX(:,2:end);
mdl2=fitlm(XXXX,y)
mdl2 =
Linear regression model: y ~ 1 + x1 + x2 + x3 + x4 Estimated Coefficients: Estimate SE tStat pValue ___________ __________ _______ _________ (Intercept) 1.8295 1.6195 1.1297 0.27636 x1 -0.094087 0.057932 -1.6241 0.12518 x2 0.03224 0.024907 1.2944 0.2151 x3 0.0088885 0.0035943 2.4729 0.025849 x4 -6.6565e-05 1.2235e-05 -5.4407 6.825e-05 Number of observations: 20, Error degrees of freedom: 15 Root Mean Squared Error: 0.907 R-squared: 0.701, Adjusted R-Squared: 0.621 F-statistic vs. constant model: 8.78, p-value = 0.000738
mdl2.Rsquared.Adjusted
ans =
1781/2869
mdl2.Rsquared.Ordinary
ans =
344/491
% Residual Analysis
contain0 = (rint(:,1)<0 & rint(:,2)>0);
idx = find(contain0==false)
idx = 0×1 empty double column vector
hold on
scatter(y,r)
scatter(y(idx),r(idx),'b','filled')
dx=.1;dy=.1;
text(y+dx,r+dy,cellstr(country_abb))
xlabel("GRO")
ylabel("Residuals")
plot([-2 5],[0 0],'--')
hold off
grid on
b
b =
4550/2113 -141/11156 278/46981 -1307/12782 242/8479 43/6437 -33/510839
k=["Intercept", "INF","BNK","GOV","SSE","TRD","INIT"]'
k = 7×1 string array
"Intercept"
"INF"
"BNK"
"GOV"
"SSE"
"TRD"
"INIT"
Coff=array2table(b');
Coff.Properties.VariableNames=k';
Coff.Properties.RowNames="Coeff";
Coff
Coff = 1×7 table
 InterceptINFBNKGOVSSETRDINIT
1 Coeff4550/2113  -141/11156 278/46981 -1307/12782 242/8479  43/6437  -33/510839
%b(7)-b(2)
NMMT=MMT;
NMMT.Country=country;
NMMT=movevars(NMMT,8,"Before",1)
NMMT = 20×8 table
 CountryINFBNKGROGOVSSETRDINIT
1 Argentina'Argentina'3246/143   3857/279   747/815   3210/217   5020/61    6560/199   204880/11    
2 Brazil'Brazil'961/129   1496/31    2956/2179  4070/211   5808/73    2882/111   81180/7     
3 Chile'Chile'9377/1079  2937/109   26875/6891  4152/481   8451/131   10665/206   130853/23    
4 Colombia'Colombia'563/297   24203/194   9608/2697  3471/248   28111/295   23511/296   206893/9     
5 Dominican Republic'Dominican Republic'133/25    2160/113   1777/2463  5003/446   28349/403   11959/192   142265/8     
6 Egypt, Arab Rep.'Egypt, Arab Rep.'6081/463   11042/277   854/229   2847/160   23780/259   22305/424   160761/11    
7 Indonesia'Indonesia'601/116   2116/55    693/884   841/36    12957/148   26712/349   209776/5     
8 Israel'Israel'3758/243   1389/35    1577/466   1559/113   94605/1193  8843/178   92819/6     
9 Korea, Rep.'Korea, Rep.'1415/717   983/14    1477/1012  5264/223   16342/165   15100/219   726065/23    
10 Malaysia'Malaysia'689/210   3332/29    6551/2005  15555/1256  5967/79    18272/109   318401/20    
11 Mexico'Mexico'9167/1950  12021/169   899/344   5362/439   15775/182   6769/103   75609/5     
12 Panama'Panama'827/130   8201/241   13638/5455  2153/147   9521/129   9272/253   210074/23    
13 Peru'Peru'3557/335   4231/109   2125/907   4699/410   21815/271   9017/188   54213/7     
14 Russian Federation'Russian Federation'14996/11397 14441/135   1360/421   578/57    7873/79    17931/49    392074/7     
15 Saudi Arabia'Saudi Arabia'1533/230   4501/66    1027/839   1853/94    50669/754   31347/532   70622/7     
16 Singapore'Singapore'1929/250   6368/207   257/107   1635/127   6788/89    12935/261   132961/10    
17 South Africa'South Africa'2955/664   8617/145   -2017/1192  1996/197   12231/136   9881/69    819959/8     
18 Turkey'Turkey'5135/1766  8392/109   419/104   2177/174   6107/95    16330/127   159616/11    
19 United Arab Emirates'United Arab Emirates'2046/233   6616/275   2476/677   21827/2240  21137/344   2825/46    65255/7     
20 Uruguay'Uruguay'1618/557   251/8     1192/329   3043/261   11093/148   6424/137   160562/25    
%writetable(NMMT,'data_col.xlsx')
%for i=1:20
% tm=join(["Country",num2str(i),".xlsx"]);
% writetable(table{i},tm)
%end
Brief summary of this function.
It uses the 2D newton method to compute the solution of a system of 2 equations
Detailed explanation of this function.
f1,f2 are the two @functions of the system given in their implicit form
x0 is a 2X1 array which contains the initial guess
MaxIter contains the max number of iterations
tol is the tolerance error
function x = Newton_multi(f1,f2,x0,MaxIter,tol)
%Computing partial derivvatives approximations
h=10e-8;
x1=x0(1);x2=x0(2);
J(1,1)=(f1(x1+h,x2)-f1(x1,x2))/h;
J(2,1)=(f2(x1+h,x2)-f2(x1,x2))/h;
J(1,2)=(f1(x1,x2+h)-f1(x1,x2))/h;
J(2,2)=(f2(x1,x2+h)-f2(x1,x2))/h;
invJ=[J(2,2) -J(1,2);-J(2,1) J(1,1)]./(J(1,1)*(J(2,2))-J(1,2)*J(2,1));
it=1;
x=x0;
while it<MaxIter && (abs(f2(x(1),x(2))-x(2))>tol || abs(f1(x(1),x(2))-x(1))>tol)
x=x-invJ*[f1(x(1),x(2));f2(x(1),x(2))];
%inverse jacobi update at x
x1=x(1);x2=x(2);
J(1,1)=(f1(x1+h,x2)-f1(x1,x2))/h;
J(2,1)=(f2(x1+h,x2)-f2(x1,x2))/h;
J(1,2)=(f1(x1,x2+h)-f1(x1,x2))/h;
J(2,2)=(f2(x1,x2+h)-f2(x1,x2))/h;
invJ=[J(2,2) -J(1,2);-J(2,1) J(1,1)]./(J(1,1)*(J(2,2))-J(1,2)*J(2,1));
%step counter
it=it+1;
end
end
function MP=marginal_profit(quantity,C,w,D)
P(quantity)=D(sum(w))*quantity-C(quantity);
MP=(diff(P,quantity));
end